//
//  KLPhotoAnimator.m
//  HealthManage
//
//  Created by 陈华 on 2017/4/10.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import "KLPhotoAnimator.h"

@interface KLPhotoAnimator () <UIViewControllerAnimatedTransitioning>

@property(nonatomic,assign) BOOL isPresented;

@end

@implementation KLPhotoAnimator


#pragma mark - UIViewControllerTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.isPresented = YES;
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.isPresented = NO;
    return self;
}


#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    self.isPresented?[self animationForPresentedView:transitionContext]:[self animationForDismissView:transitionContext];
}


#pragma mark - 自定义动画相关方法
/**显示动画*/
- (void)animationForPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    //取出弹出的view
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    //将presentedView添加到containerView中
    [[transitionContext containerView] addSubview:presentedView];
    
    //获取执行动画的imageView及rect
    CGRect startRect = [self.presentedDelegate startRect:self.indexPath];
    UIImageView *imageView = [self.presentedDelegate imageView:self.indexPath];
    [[transitionContext containerView] addSubview:imageView];
    imageView.frame = startRect;
    
    //执行动画
    presentedView.alpha = 0.0;
    [transitionContext containerView].backgroundColor = [UIColor blackColor];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        imageView.frame = [self.presentedDelegate endRect:self.indexPath];
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        presentedView.alpha = 1.0;
        [transitionContext containerView].backgroundColor = [UIColor clearColor];
        [transitionContext completeTransition:YES];
    }];
    
}

/**消失动画*/
- (void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    //取出即将消失的view,从容器视图中移除
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    [dismissView removeFromSuperview];
    
    //获取执行动画的imageView
    UIImageView *imageView = [self.dismissDelegate imageViewForDimissView];
    [[transitionContext containerView] addSubview:imageView];
    NSIndexPath *indexPath = [self.dismissDelegate indexPathForDismissView];
    imageView.frame = [self.presentedDelegate endRect:indexPath];
    
    //执行动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        imageView.frame = [self.presentedDelegate startRect:indexPath];
        
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
    
}


@end

















